﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using WPGame.Graphics;

namespace WPGame.Objects
{
    /*
     * Hướng dẫn dùng:
     * Tạo một Button(tọa độ, Sprite bình thường, [Sprite khi chọn (có hoặc ko cũng được)])
     * Muốn button đó thực hiện chức năng gì thì:
            - Tạo hàm đó kiểu void không tham số
            - Truyền hàm mới tạo vào doAction của Button
            - Ví dụ coi ở MenuState.cs
     */
    class Button
    {
        private Sprite m_NormalButton;
        private Sprite m_SelectedButton;
        private Vector2 m_Position;
        private Rectangle m_Bound;
        private GInputState m_Input;
        private eGStatusButton m_Status;

        public Button(Vector2 position, Sprite normalbutton, Sprite selectedbutton = null)
        {
            m_NormalButton = normalbutton;
            m_NormalButton.Position = position;

            m_Position = m_NormalButton.Position;
            m_Bound = normalbutton.Bound;
            m_Bound.Location = new Point((int)position.X, (int)position.Y);

            m_Status = eGStatusButton.NORMAL;
            
            m_SelectedButton = selectedbutton;
            if(m_SelectedButton != null)
                m_SelectedButton.Position = position;

        }

        public void HandleInput(GInputState input)
        {
            m_Input = input;
            foreach (TouchLocation touchLocation in m_Input.TouchState)
            {
                if (touchLocation.State == TouchLocationState.Pressed)
                {
                    if (m_Bound.Contains((int)touchLocation.Position.X, (int)touchLocation.Position.Y))
                    {
                        if(m_Status == eGStatusButton.NORMAL)
                            m_Status = eGStatusButton.SELECTED;
                        else if(m_Status == eGStatusButton.SELECTED)
                            m_Status = eGStatusButton.NORMAL;

                        //run Action
                        if (actionFunc != null)
                            actionFunc();
                    }
                }
            }
        }

        public void Render(SpriteBatch spriteBatch)
        {
            if (m_Status == eGStatusButton.NORMAL || m_SelectedButton == null)
            {
                m_NormalButton.Render(spriteBatch);
            }
            else if (m_SelectedButton != null && m_Status == eGStatusButton.SELECTED)
            {
                m_SelectedButton.Render(spriteBatch);
            }
        }

        public delegate void Action();
        public Action actionFunc = null;

        #region Property

        public Vector2 Position
        {
            get { return m_Position; }
            set 
            {
                m_Position = value;
                m_NormalButton.Position = m_Position;
            }
        }

        public Rectangle Bound
        {
            get { return m_Bound; }
            set { Bound = value; }
        }


        public GInputState Input
        {
            get { return m_Input; }
            set { m_Input = value; }
        }

        public eGStatusButton Status
        {
            get { return m_Status; }
            set { m_Status = value; }
        }

        #endregion
    }
}
